<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/10/20
 * Time: 下午3:13
 */
class SceCommonController extends BaseController {
    /*
     * 车牌省份信息
     * */
    public function car_no_area(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            $car_no_area = ResourceProvinceType::where('status',1)->select(array('id','short_name'))->get()->toArray();
            $count = count($car_no_area);
            $result = array(
                'count' => $count,
                'car_no_area' => $car_no_area
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "car_no_area", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-02-22 16:43:43
     * @Description: 用户技师端系统的升级 
     */
    public function app_version(){
        $validator = Validator::make(Input::all(),
            array(
                'version_code' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            if(Input::get("version_code")!=Config::get("version_code.version")){
                $result=array("is_upgrade"=>1,"version_code"=>Config::get("app_version.version"),"url"=>Config::get("app_version.url"));
            }else{
                $result=array("is_upgrade"=>0,"version_code"=>Config::get("app_version.version"),"url"=>Config::get("app_version.url"));
            }            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "car_no_area", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-7-11 18:35:29
     * @Description: 用户技师端系统的升级
     */
    public function app_update(){
        $validator = Validator::make(Input::all(),
            array(
                'version' => 'required',
            )
        );
    
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            $ou=OnlineUpdate::orderBy('created_at','desc')->first();
            if($ou){
                $url=Config::get("config.apk_url").$ou->url;
                $result=array("is_upgrade"=>$ou->force_update,"version"=>$ou->version,"url"=>$url);
            }else{
                $result=array("is_upgrade"=>0,"version"=>'',"url"=>'');
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "app_update", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-3-14 15:03:43
     * @Description: 技师端报错信息优化接口
     */
    public function sce_error_log(){
        $validator = Validator::make(Input::all(),
            array(
                'imei' => 'required',
                'interface_name' => 'required',
                'interface_log' => 'required',
            )
        );
    
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            $sel=new SceErrorLog;
            $sel->imei=Input::get('imei');
            $sel->interface_name=Input::get('interface_name');
            $sel->interface_log=Input::get('interface_log');
            $sel->token=Input::get('token');
            $sel->save();
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "sce_error_log", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /*
     * 是否显示vin  是否显示机油
     */
    function car_show_vin () {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            $result = [];
            $result['isShowVIN'] = Config::get('config.is_show_vin');
            $result['isShowOil'] = Config::get('config.is_show_oil');
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
            
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "car_show_vin", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}